package com.example.star.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.star.R;
import com.example.star.adapter.MessageAdapter;
import com.example.star.api_service.MessageApi;
import com.example.star.entity.Message;
import com.example.star.entity.User;
import com.example.star.retrofit.ApiClient;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MessageActivity extends AppCompatActivity {

    public static final String EXTRA_SINGER_NAME = "extra_singer_name";

    private EditText messageEditText;
    private Button sendButton;
    private ListView messageListView;
    private TextView starNameTextView;
    private String starName;
    private MessageAdapter messageAdapter;
    private String userName; // 用于存储用户名

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        // 获取明星姓名
        Intent intent = getIntent();
        starName = intent.getStringExtra(EXTRA_SINGER_NAME);
        if (starName == null) {
            starName = "未知歌手";
        }

        // 从 SharedPreferences 中读取用户名
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        userName = sharedPreferences.getString("username", "");

        initViews();

        // 设置明星姓名到 TextView
        starNameTextView.setText(starName);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });

        getMessages();
    }

    private void initViews() {
        messageEditText = findViewById(R.id.messageEditText);
        sendButton = findViewById(R.id.sendButton);
        messageListView = findViewById(R.id.messageListView);
        starNameTextView = findViewById(R.id.star_name_text_view);
    }

    private void sendMessage() {
        String messageText = messageEditText.getText().toString();
        if (messageText.isEmpty()) {
            Toast.makeText(this, "请输入消息", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建消息对象
        Message message = new Message();
        message.setStarName(starName);
        message.setUserName(userName);
        // 这里需要根据实际情况创建 User 对象并设置到 Message 中
        User user = new User();
        user.setUsername(userName);
        message.setUser(user);
        message.setMessage(messageText);

        // 调用 API 发送消息
        MessageApi messageApi = ApiClient.getMessageApi();
        Call<Void> call = messageApi.sendMessage(message);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(MessageActivity.this, "消息发送成功", Toast.LENGTH_SHORT).show();
                    messageEditText.setText("");
                    getMessages();
                } else {
                    Toast.makeText(MessageActivity.this, "消息发送失败: " + response.code(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                Toast.makeText(MessageActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getMessages() {
        MessageApi messageApi = ApiClient.getMessageApi();
        Call<List<Message>> call = messageApi.getMessagesByStarName(starName);
        call.enqueue(new Callback<List<Message>>() {
            @Override
            public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {
                if (response.isSuccessful()) {
                    List<Message> messages = response.body();
                    messageAdapter = new MessageAdapter(MessageActivity.this, messages);
                    messageListView.setAdapter(messageAdapter);
                } else {
                    Toast.makeText(MessageActivity.this, "获取消息失败: " + response.code(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Message>> call, Throwable t) {
                Toast.makeText(MessageActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}